import { observable, action, computed } from 'mobx'
import { ConfigDB } from '../../config/ConfigDB';
import { SheetName } from '../../enum/SheetName';
import { LoadManager } from '../managers/LoadManager';
import MusicManager from '../managers/MusicManager';
import IStore from './IStore';
import { saveData, UserStore } from './UserStore';

export class WordData {

    @observable
    selected: boolean = false;
    @observable
    idx: number = -1;
    @observable
    key: string = '';
    constructor() {

    }
}
/**
 * 游戏运行数据管理类
 */
export class GamingStore implements IStore {

    prefix: string = 'userScoreInfo'
    private static _ins: GamingStore;

    public static get Ins() {
        return this._ins = this._ins ? this._ins : new GamingStore();
    }

    /**当前关卡原始数据 */
    private curLevleWords: string[];

    /**当前关卡所有的字 */
    @observable
    public allLevelWords: {} = {};

    /**当前选中的字 */
    @observable
    public selectedWords: WordData[] = [];

    /**当前完成的成语 */
    @observable
    public completedWords: string[] = [];

    /**当前选中是否已满 */
    @computed
    get isFull() {
        return this.selectedWords.length >= 4;
    }

    /**当前是否还未选中 */
    @computed
    get isZero() {
        return this.selectedWords.length == 0;
    }
    /**是否通关 */
    @computed
    get isPass() {
        return this.completedWords.length == this.curLevleWords.length;
    }

    /**重新开始 */
    @action
    reset() {
        this.allLevelWords = [];
        this.selectedWords = [];
        this.completedWords = [];
        this.init();
    }
    /**清空所有选中 */
    @action
    clear() {
        this.selectedWords.forEach((wordInfo) => {
            wordInfo.selected = false;
            this.allLevelWords[wordInfo.idx] = wordInfo;
        })
        this.selectedWords = [];
    }

    @computed
    get success() {
        let str = '';
        this.selectedWords.map((value) => {
            str += value.key;
        })
        return this.curLevleWords.indexOf(str) > -1;
    }

    /**验证 */
    @action
    async sure() {
        let str = '';
        this.selectedWords.map((value) => {
            str += value.key;
        })
        let has = this.curLevleWords.indexOf(str) > -1;
        if (str.length) {
            if (has) {
                this.selectedWords = [];
                this.completedWords.push(str);
                let music = await LoadManager.loadMusicEffect('sounds/ok')
                MusicManager.playEffect(music, false);
            } else {
                let music = await LoadManager.loadMusicEffect('sounds/error')
                MusicManager.playEffect(music, false);
            }
        }
    }

    @action
    public init() {
        let str = '';
        let words: Iwords[] = ConfigDB.getTable(SheetName.words).items;
        this.curLevleWords = words[UserStore.Ins.level].words;
        this.curLevleWords.forEach((value) => {
            str += value
        })
        let newWords = !UserStore.Ins.level ? str.split('') : this.shuffle(str);
        this.genWordData(newWords)
    }
    public get save() {
        return null;
    }

    parse() {

    }
    /**
     * 字符串乱序重排
     * @param str 字符串
     */
    shuffle(str: string) {
        let arr = str.split('');
        let temp = null;
        let length = arr.length;
        for (let i = 0; i < length - 1; i++) {
            let index = Math.floor(Math.random() * (length--));
            temp = arr[index];
            arr[index] = arr[length];
            arr[length] = temp;
        }
        return arr;
    }
    /**
     * 生成游戏数据
     * @param words 
     */
    genWordData(words: string[]) {
        words.forEach((value: string, index: number) => {
            let data = new WordData();
            data.selected = false;
            data.idx = index;
            data.key = value;
            this.allLevelWords[index] = data;
        })
    }


}
window['GamingStore'] = this;